/**
 * @描述：1 先用这个生成表节点
 * @作者：韦
 * @时间：2019/3/31
 */
public class TableUtil {

    public static void main(String[] args) {

        // 先根据表名生成表节点，然后复制节点内容到 generatorConfig.xml 最末位

        // MySql 查询所有表名，并用逗号分割
        // SELECT GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema='xsl_oa' AND table_type='base table';

        String tableNames = "t_user";

        String[] tableNameArr = tableNames.split(",");

        for (String tableName : tableNameArr) {

            System.out.println("<table tableName=\"" + tableName + "\" alias=\"T1\" domainObjectName=\"" + toCamelCase(tableName) + "DO\" mapperName=\"" + toCamelCase(tableName) + "Mapper\"");
            System.out.println("       enableCountByExample=\"false\" enableUpdateByExample=\"false\"");
            System.out.println("       enableDeleteByExample=\"false\" enableSelectByExample=\"false\"");
            System.out.println("       selectByExampleQueryId=\"false\">");
            System.out.println("    <property name=\"selectAllOrderByClause\" value=\"id\"/>");
            System.out.println("    <generatedKey column=\"id\" sqlStatement=\"Mysql\" identity=\"true\"/>");
            System.out.println("    <columnRenamingRule searchString=\"is_\" replaceString=\"\"/>");
            System.out.println("</table>");
        }


    }

    /* 下划线转驼峰命名 */
    public static String toCamelCase(String tableName) {

        char[] chars = tableName.toCharArray();

        char[] result = new char[chars.length];
        int len = 0;

        for (int i = 0; i < chars.length; i++, len++) {
            if (i == 0){
                char nextChar = chars[i];

                if (nextChar >= 97 && nextChar <= 122) {

                    result[len] = (char) (nextChar - 32);
                    continue;
                }
            }
            if ('_' == chars[i]) {

                char nextChar = chars[++i];

                if (nextChar >= 97 && nextChar <= 122) {

                    result[len] = (char) (nextChar - 32);
                    continue;
                }


            }
            result[len] = chars[i];
        }

        return new String(result, 0, len);

    }

}
